<?php

namespace App\Http\Controllers\System;


use App\Events\CloudEvent;
use App\Model\System\Eeofile;
use Illuminate\Http\Request;
use App\Http\Controllers\MessagePacket;
use Laravel\Lumen\Routing\Controller as BaseController;


class CloudController extends BaseController
{
    use MessagePacket;


    function  __construct(){}


    /**
     * 获取云盘内容列表
     * @param Request $request
     * @return array
     * @throws \Exception
     */
    function getCloud(Request $request)
    {
        try{
            $event = New CloudEvent(CloudEvent::READ, CloudEvent::FOLDER, 0);
            event($event);
            $results = $event->getResults();
            return $this->success($results);
        }catch (\Exception $e){
            return $this->fail(false,"获取云盘列表失败：".$e->getMessage());
        }
    }

    /**
     * 获取指定文件夹下等所有内容
     * @param Request $request
     * @param $fid
     * @return array
     */
    function readFolder(Request $request, $fid)
    {
        try{
            $event = New CloudEvent(CloudEvent::READ, CloudEvent::FOLDER, $fid);
            event($event);
            $results = $event->getResults();
            return $this->success($results);
        }catch (\Exception $e){
            return $this->fail(false,"获取云盘列表失败：".$e->getMessage());
        }
    }

    /**
     * 创建文件夹
     * @param Request $request
     * @return array
     * @throws \Exception
     */
    function makeFolder(Request $request)
    {
        try{
            # 判断参数
            if(!$request->has('pid') or !$request->has('name') ){
                return $this->fail(false, '参数不全');
            }
            # do
            $pid = $request->get('pid');
            $event = New CloudEvent(CloudEvent::MAKE, CloudEvent::FOLDER, 0, $pid);
            $event->setName($request->get('name'));

            event($event);
            $results = $event->getResults();
            return $this->success($results);
        }catch (\Exception $e){
            return $this->fail(false,"创建云盘目录失败：".$e->getMessage());
        }
    }

    /**
     * 删除文件夹
     * @param Request $request
     * @param $fid
     * @return array
     * @throws \Exception
     */
    function removeFolder(Request $request, $fid)
    {
        try{
            $event = New CloudEvent(CloudEvent::REMOVE, CloudEvent::FOLDER, $fid);
            event($event);
            $results = $event->getResults();
            return $this->success($results);
        }catch (\Exception $e){
            return $this->fail(false,"删除文件夹失败：".$e->getMessage());
        }
    }

    /**
     * 重命名文件夹
     * @param Request $request
     * @param $fid
     * @return array
     */
    function renameFolder(Request $request, $fid)
    {
        try{
            # 判断参数
            if(!$request->has('name') ){
                return $this->fail(false, '参数不全');
            }
            # do
            $event = New CloudEvent(CloudEvent::RENAME, CloudEvent::FOLDER, $fid);
            $event->setName($request->get('name'));
            event($event);
            $results = $event->getResults();
            return $this->success($results);
        }catch (\Exception $e){
            return $this->fail(false);
        }
    }


    /**
     * 上传文件
     * @param Request $request
     * @return array
     */
    function uploadFile(Request $request)
    {
        try{
            $name = 'Filedata';
            if(!$request->hasFile($name) or !$request->has('pid')){
                return $this->fail(null, '信息不全');
            }

            # 获取文件
            $fileName = '';
            $file = $request->file($name);
            $move2path = Eeofile::makeUploadPath($file);
            $existedFile = Eeofile::getUploadFileFullName($file);
            if(file_exists($existedFile)){
                $fileName = $existedFile;
            }else{
                $target = $file->move($move2path, $file->getClientOriginalName());
                $fileName =  $target->getRealPath();
            }

            # 创建事件
            $event = New CloudEvent(CloudEvent::MAKE, CloudEvent::FILE, 0, $request->get('pid'));
            $event->setFile($fileName);

            # 抛出事件
            event($event);
            $results = $event->getResults();
            return $this->success($results);
        }catch (\Exception $e){
            return $this->fail(false,"上传文件失败：".$e->getMessage());
        }
    }


    /**
     * 重命名文件
     * @param Request $request
     * @param $fid
     * @return array
     */
    function renameFile(Request $request, $fid)
    {
        try{
            # 判断参数
            if(!$request->has('name') ){
                return $this->fail(false, '参数不全');
            }

            $event = New CloudEvent(CloudEvent::RENAME, CloudEvent::FILE, $fid);
            $event->setName($request->get('name'));

            event($event);
            $results = $event->getResults();
            return $this->success($results);
        }catch (\Exception $e){
            return $this->fail(false);
        }
    }

    /**
     * 删除文件
     * @param Request $request
     * @param $fid
     * @return array
     */
    function removeFile(Request $request, $fid)
    {
        try{
            $event = New CloudEvent(CloudEvent::REMOVE, CloudEvent::FILE, $fid);
            event($event);
            $results = $event->getResults();
            return $this->success($results);
        }catch (\Exception $e){
            return $this->fail(false);
        }
    }



}
